<template>
  <div class="orderMange">
    <el-form :model="form" class="demo-form-inline form-ml">
      <el-form-item label="订单号">
        <el-input style="width: 200px;" v-model="form.number" placeholder="请输入订单号"></el-input>
      </el-form-item>
      <el-form-item label="赛事ID">
        <el-input style="width: 200px;" v-model="form.com_id" placeholder="请输入赛事ID"></el-input>
      </el-form-item>
      <el-form-item label="个人信息">
        <el-input style="width: 260px;" v-model="form.userInfo" placeholder="请输入姓名/手机号/身份证号"></el-input>
      </el-form-item>
      <el-form-item label="支付状态">
        <el-select v-model="form.payState" style="width: 185px;" placeholder="已支付" @change="get_order_list">
          <el-option label="全部" value="0"></el-option>
          <el-option label="已支付" value="1"></el-option>
          <el-option label="未支付" value="3"></el-option>
          <el-option label="已退款" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="时间" style="width: 40%;">
        <el-date-picker v-model="form.date" format="yyyy - MM - dd" value-format="yyyy-MM-dd" type="daterange"
          range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="退款" style="width: 16%;margin-left: 19px;">
        <el-select v-model="form.apply_refund" style="width: 110px;" placeholder="全部" @change="get_order_list">
          <el-option label="全部" value="0"></el-option>
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="width: 8%;text-align: right;">
        <el-button type="primary" @click="onEnterSubmit">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="MatchShowList" class="MatchTable" stripe :default-sort="{ prop: 'update_time', order: 'descending' }">
      <el-table-column label="订单号" prop="order_num" width="150" sortable></el-table-column>
      <el-table-column label="姓名" prop="name" width="100"></el-table-column>
      <el-table-column label="性别" prop="sex" width="60"></el-table-column>
      <el-table-column label="手机号" prop="phone" width="120"></el-table-column>
      <el-table-column label="证件号" prop="id_card" width="180"></el-table-column>
      <el-table-column label="赛事ID" prop="com_id" width="80">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.com_name" placement="top">
            <router-link style="text-decoration: none;color: #66b1ff;font-weight: bold;" :to="{ path: '/index/match/MatchDetail', query: { MatchId: scope.row.com_id } }">
              {{ scope.row.com_id }}
            </router-link>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="组别项目" width="120">
        <template slot-scope="scope">
          <router-link style="text-decoration: none;color: #66b1ff;font-weight: bold;" :to="{ path: '/index/match/MatchSignUp', query: { MatchId: scope.row.com_id, teamId: scope.row.group_id } }">
            {{ scope.row.group_name }}
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="价格" prop="price" width="60"></el-table-column>
      <el-table-column label="申请退款" prop="apply_refund_status" width="80">
        <template slot-scope="scope">
          <div style="width: 100%;text-align: center;">
            <span v-if="scope.row.apply_refund == 0">{{ scope.row.apply_refund_status }}</span>
            <span style="text-decoration: none;color: #66b1ff;font-weight: bold;cursor: pointer;" v-else @click="apply_refund(scope.row.order_num, scope.$index)">{{ scope.row.apply_refund_status }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="order_status" width="80">
        <template slot-scope="scope">
          <span :class="{ class_paid: scope.row.order_status == '已支付', class_Unpaid: scope.row.order_status == '未支付', class_Refunded: scope.row.order_status == '已退款' }">
            {{ scope.row.order_status }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作时间" prop="update_time" width="200"></el-table-column>
    </el-table>
    <div class="footer">
      <div>
      <span>总计：{{ PagesAll }}条</span>
      <span style="margin: 0 20px;">已支付：{{ paid }}元</span>
      <span>已退款：{{ Refunded }}元</span>
      </div>
      <el-pagination background layout="prev, pager, next" :total="PagesAll" @current-change="ChangePage" style="          margin-top: -26px;
            margin-bottom: 40px;
            margin-left: 345px;"></el-pagination>
    </div>
  </div>
</template>

<script>
  import { mapState } from 'vuex';
  import debounce from '../../utils/debounce'
  export default {
    data() {
      return {
        form: {
          number: '',
          com_id: '',
          userInfo: '',
          payState: '',
          apply_refund: '',
          date: []
        },
        PagesAll: null,
        MatchShowList: [],
        currentPage: 1,
        loading: false,
        total: 0,
        paid: 0,
        Refunded: 0,
        debounceFuc: debounce(this.onEnterSubmit, 1000)
      };
    },
    created() {
      this.get_order_list();
    },
    watch: {
      'form.number': function (newValue, oldValue) {
        this.debounceFuc();
      },
      'form.com_id': function (newValue, oldValue) {
        this.debounceFuc();
      },
      // 'form.userInfo': function (newValue, oldValue) {
      //   this.debounceFuc();
      // }
    },
    methods: {
      onEnterSubmit() {
        this.get_order_list();
        return false;
      },
      get_order_list() {
        let time = [];
        if (this.form.date && this.form.date.length == 2) {
          let start = `${this.form.date[0]} 00:00:00`;
          let end = `${this.form.date[1]} 23:59:59`;
          time.push(start, end);
        }
        this.loading = true;
        this.$axios
          .post(
            '/OrderManage/index', {
              page: this.currentPage,
              order_no: this.form.number,
              com_id: this.form.com_id,
              key_word: this.form.userInfo,
              order_status: this.form.payState,
              time: time,
              apply_refund: this.form.apply_refund
            }, {
              headers: {
                token: window.sessionStorage.getItem('token')
              }
            }
          )
          .then(res => {
            const {
              code,
              data,
              msg
            } = res.data;
            if (code == 2000) {
              this.MatchShowList = data.list;
              this.PagesAll = data.count_order;
              this.paid = data.pay;
              this.Refunded = data.refund;
              this.loading = false;
            } else {
              this.$message({
                type: 'error',
                message: msg
              });
            }
          })
          .catch(error => {
            this.$message({
              type: 'error',
              message: '请求失败'
            });
          });
      },
      ChangePage(page) {
        this.currentPage = page;
        this.get_order_list();
      },
      //申请退款
      apply_refund(order_num, index) {
        this.$confirm('您执行了退款操作, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(() => {
            this.$axios
              .post(
                '/Orderpay/wx_refund', {
                  order_num: order_num
                }, {
                  headers: {
                    token: window.sessionStorage.getItem('token')
                  }
                }
              )
              .then(res => {
                if (res.data.code == 2000) {
                  this.MatchShowList[index].order_status = '已退款'
                  this.$message({
                    message: res.data.msg,
                    type: 'success'
                  });
                } else {
                  this.$message({
                    message: res.data.msg,
                    type: 'error'
                  });
                }
              });
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消退款操作'
            });
          });
      }
    }
  };
</script>

<style scoped>
.orderMange{
  padding-bottom: 200px;
}
.footer{
  margin-left:150px;
}
  .class_paid {
    color: #259b24;
  }

  .class_Unpaid {
    color: #ff9800;
  }

  .class_Refunded {
    color: #e51c23;
  }

  .form-ml {
    width: 1000px;
    display: flex;
    flex-wrap: wrap;
    /* justify-content: space-between; */
    margin-left: 10px;
  }

  .form-ml>div {
    width: 33%;
  }

  .orderMange {
    margin-left: 26px;
    margin-top: 50px;
    margin-bottom: 20px;
  }

  .form {
    display: flex;
    margin-left: 26px;
    margin-top: 50px;
    margin-bottom: 20px;
  }

  .state {
    width: 200px;
  }

  .keyword {
    display: flex;
    width: 410px;
  }

  .keyword .el-form-item {
    margin-bottom: 0;
  }

  .search {
    width: 123px;
    text-align: right;
  }

  .MatchTable {
    width: 95%;
    margin-top: 0px;
    margin-bottom: 50px;
  }
</style>
